#include "global.h"
#include "tagcoloreditor.h"
#include "coloreditor.h"
#include "opacityeditor.h"

TagColorEditor::TagColorEditor()
{
  createGUI();
}

void
TagColorEditor::setColors()
{
  uchar *colors = Global::tagColors();
  for (int i=0; i < 254; i++)
    {      
      int r,g,b;
      float a;

      r = colors[4*(i+1)+0];
      g = colors[4*(i+1)+1];
      b = colors[4*(i+1)+2];
      a = (float)colors[4*(i+1)+3]/255.0;

      //a = (int)a + (int)((a-(int)a)*10)*0.1;

      QTableWidgetItem *colorItem = new QTableWidgetItem;
      colorItem->setData(Qt::DisplayRole, QColor(r,g,b));
      colorItem->setData(Qt::DecorationRole, QColor(r,g,b));
      
      QTableWidgetItem *opacityItem = new QTableWidgetItem;
      opacityItem->setData(Qt::DisplayRole, a);

      table->setItem(i, 0, colorItem);
      table->setItem(i, 1, opacityItem);
    }
}

void
TagColorEditor::createGUI()
{
  QItemEditorFactory *colorFactory = new QItemEditorFactory;
  QItemEditorCreatorBase *colorCreator =
    new QStandardItemEditorCreator<ColorEditor>();
  colorFactory->registerEditor(QVariant::Color, colorCreator);
  
  QItemEditorFactory *opFactory = new QItemEditorFactory;
  QItemEditorCreatorBase *opacityCreator =
    new QStandardItemEditorCreator<OpacityEditor>();
  opFactory->registerEditor(QVariant::Double, opacityCreator);


  table = new QTableWidget(254, 2);
  table->setHorizontalHeaderLabels(QStringList() << tr("Color")
				                 << tr("Opacity"));
  table->resize(150, 200);
  
  QItemDelegate *colorDelegate = new QItemDelegate();
  colorDelegate->setItemEditorFactory(colorFactory);
  
  QItemDelegate *opDelegate = new QItemDelegate();
  opDelegate->setItemEditorFactory(opFactory);
  
  table->setItemDelegateForColumn(0, colorDelegate);
  table->setItemDelegateForColumn(1, opDelegate);
  
  table->setEditTriggers(QAbstractItemView::DoubleClicked);
  
  setColors();

  table->setGridStyle(Qt::NoPen);
  for (int i=0; i < table->rowCount(); i++)
    table->setRowHeight(i, 20);

  table->setColumnWidth(0, 100);
  table->setColumnWidth(1, 50);
    
  QVBoxLayout *layout = new QVBoxLayout;
  layout->addWidget(table);
  
  setLayout(layout);
  
  setWindowTitle(tr("Tag Color Editor"));
  
  connect(table, SIGNAL(itemChanged(QTableWidgetItem*)),
	  this, SLOT(itemChanged(QTableWidgetItem*)));
}

void
TagColorEditor::itemChanged(QTableWidgetItem *item)
{
  uchar *colors = Global::tagColors();

  int row = item->row();
  int colm = item->column();
  if (colm == 0)
    {
      QColor clr;
      clr.setNamedColor(item->data(Qt::DisplayRole).toString());
      item->setData(Qt::DecorationRole, clr);
      colors[4*(row+1)+0] = clr.red();
      colors[4*(row+1)+1] = clr.green();
      colors[4*(row+1)+2] = clr.blue();
    }
  else
    {
      double opv = item->data(Qt::DisplayRole).toDouble();
      int op = opv*255;
      op = qMin(op, 255);
      colors[4*(row+1)+3] = op;
    }

  emit tagColorChanged();
}


void
TagColorEditor::newColorSet(int h)
{
  qsrand(h);

  uchar *colors = Global::tagColors();

  for(int i=0; i<256; i++)
    {
      float r,g,b;
      if (i > 0 && i < 255)
	{
	  r = (float)qrand()/(float)RAND_MAX;
	  g = (float)qrand()/(float)RAND_MAX;
	  b = (float)qrand()/(float)RAND_MAX;
	}
      else
	{
	  r = g = b = 0;
	}
      colors[4*i+0] = 255*r;
      colors[4*i+1] = 255*g;
      colors[4*i+2] = 255*b;
    }
  
  setColors();
}

void
TagColorEditor::setOpacity(float op)
{
  uchar *colors = Global::tagColors();

  for(int i=0; i<256; i++)
    {
      int a;
      if (i > 0 && i < 255)
	a = 255*op;
      else
	a = 0;
      a = qMin(a, 255);
      colors[4*i+3] = a;
    }
  
  setColors();
}
